/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThumbmailDialog.java
 *
 * Created on 11.8.2010, 13:30:54
 */
package figoo;

import com.mortennobel.imagescaling.ResampleOp;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Lada Riha
 */
public class ThumbmailDialog extends javax.swing.JDialog {

    /** Creates new form ThumbmailDialog */
    public ThumbmailDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int w = this.getSize().width;
        int h = this.getSize().height;

        int left = (d.width - w) / 2;
        int top = (d.height - h) / 2;
        this.setLocation(left, top);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(figoo.FigooApp.class).getContext().getResourceMap(ThumbmailDialog.class);
        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(368, 368));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    void doPdfThumbmail(File file) {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
            PDFFile pdffile = new PDFFile(buf);
            PDFPage page = pdffile.getPage(0);
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
            Image img = page.getImage(368, 368, rect, null, true, true);
            JLabel jLabel17 = (new JLabel(new ImageIcon(img)));
            jPanel1.add(jLabel17);
            jPanel1.setSize(368, 368);
            this.pack();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void doImageThumbmail(File file) {

        try {
            ImageInputStream imageStream = ImageIO.createImageInputStream(file);
            Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
            ImageReader reader = null;
            if (!readers.hasNext()) {
                throw new Exception("Cannot read image file");
            } else {
                reader = readers.next();
            }
            reader.setInput(imageStream, true, true);
            int final_width = reader.getWidth(0);
            int final_height = reader.getHeight(0);
            reader.dispose();
            imageStream.close();
            int newHeight = 368;
            int newWidth = 368;

            float change = change = (float) ((float) final_width / (float) newWidth);
            newHeight = (int) (final_height / change);
            if (final_height >= final_width) {
                newHeight = 368;
                change = (float) ((float) final_height / (float) newHeight);
                newWidth = (int) (final_width / change);
            }
            BufferedImage originalImage = ImageIO.read(file);
            ResampleOp resampleOp = new ResampleOp(newWidth, newHeight);
            BufferedImage resizedImage = resampleOp.filter(originalImage, null);
            JLabel jLabel17 = (new JLabel(new ImageIcon(resizedImage)));
            jPanel1.add(jLabel17);
            jPanel1.setSize(newWidth, newHeight);
            jPanel1.setPreferredSize(new Dimension(newWidth, newHeight));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.pack();

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
